home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / Calculator.java < prev    next >
Text File  |  1998-06-30  |  6KB  |  247 lines

  1. package com.sun.java.swing;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. import java.util.*;
  7. import java.text.*;
  8.  
  9. public class Calculator extends Container implements ActionListener
  10. {
  11.   private JLabel output;
  12.   private JLabel memDisplay;
  13.   private TextField opDisplay;
  14.   private int digitsTyped = 0;
  15.  
  16.   private double numMemory;
  17.   private int operatorMemory = -1;
  18.   private boolean decimalOn = false;
  19.   private JButton OK;
  20.  
  21.   public boolean useValue;
  22.   
  23.   protected GridBagLayout layout = new GridBagLayout();
  24.   protected GridBagConstraints cons = new GridBagConstraints();
  25.    
  26.   public Calculator()
  27.   {
  28.     setLayout(new BorderLayout());
  29.     output = new JLabel("0");
  30.     output.setHorizontalAlignment(output.RIGHT);
  31.     JPanel panel1 = new JPanel();
  32.     JPanel panel2 = new JPanel();
  33.     JPanel panel3 = new JPanel();
  34.     panel1.setLayout(new GridLayout(4,3));
  35.     indexedButton tempElt;
  36.     JButton tempButton;
  37.     numArrayListener numListener = new numArrayListener();
  38.     opArrayListener opListener = new opArrayListener();
  39.     int i,j;
  40.     
  41.     for(i=3; i>0; i--)
  42.     {
  43.     for(j=2; j>=0; j--)
  44.       {
  45.         
  46.         tempElt = new indexedButton(Integer.toString(3*i - j),3*i-j);
  47.         tempElt.setMargin(new Insets(1,1,1,1));
  48.         panel1.add(tempElt);
  49.         tempElt.addActionListener(numListener);
  50.       }
  51.     }
  52.     tempElt = new indexedButton("0",0);
  53.     panel1.add(tempElt);
  54.     tempElt.addActionListener(numListener);
  55.     
  56.     tempButton = new JButton(".");
  57.     panel1.add(tempButton);
  58.     tempButton.addActionListener(this);
  59.     tempButton = new JButton("=");
  60.     panel1.add(tempButton);
  61.     tempButton.addActionListener(this);
  62.     
  63.     panel2.setLayout(new GridLayout(4,1));
  64.     tempElt = new indexedButton("*",0);
  65.     panel2.add(tempElt);
  66.     tempElt.addActionListener(opListener);
  67.     tempElt = new indexedButton("/",1);
  68.     panel2.add(tempElt);
  69.     tempElt.addActionListener(opListener);
  70.     tempElt = new indexedButton("+",2);
  71.     panel2.add(tempElt);
  72.     tempElt.addActionListener(opListener);
  73.     tempElt = new indexedButton("-",3);
  74.     panel2.add(tempElt);
  75.     tempElt.addActionListener(opListener);
  76.  
  77.     panel3.setLayout(new GridLayout(1,3));
  78.     tempButton = new JButton("Clear");
  79.     panel3.add(tempButton);
  80.     tempButton.addActionListener(this);
  81.     tempButton = new JButton("Cancel");
  82.     panel3.add(tempButton);
  83.     tempButton.addActionListener(this);
  84.  
  85.     OK = new JButton("OK");
  86.     panel3.add(OK);
  87.  
  88.     cons.gridx = 0;
  89.     cons.gridy = 0;
  90.     cons.weightx = 1;
  91.     cons.weighty = 1;
  92.     cons.insets = new Insets(10,10,10,10);
  93.     JPanel panel4 = new JPanel();
  94.     //panel4.setLayout(layout);
  95.     panel4.setLayout(new BorderLayout());
  96.     GridBagConstraints cons = this.cons;
  97.     cons.insets = new Insets(0,0,0,0);
  98.     cons.gridx = GridBagConstraints.RELATIVE; 
  99.     memDisplay = new JLabel("(Memory empty)");
  100.     opDisplay = new TextField(1);
  101.     opDisplay.setEditable(false);
  102.     
  103.     panel4.add(memDisplay, BorderLayout.WEST);
  104.     panel4.add(opDisplay, BorderLayout.EAST);
  105.     panel4.add(output, BorderLayout.SOUTH);
  106.  
  107.     add(panel4, BorderLayout.NORTH);
  108.     add(panel1, BorderLayout.WEST);
  109.     add(panel2, BorderLayout.EAST);
  110.     add(panel3, BorderLayout.SOUTH);
  111.     
  112.   }
  113.   public double convertOutput()
  114.   {
  115.     double num;
  116.     if(!(decimalOn))
  117.     //if(output.getText().indexOf('.') == -1)
  118.       {
  119.     num = (double)(Integer.parseInt(output.getText()));
  120.       }
  121.     else
  122.       {
  123.     num = (Double.valueOf(output.getText())).doubleValue();    
  124.       }
  125.     return num;
  126.   }
  127.   public void clear()
  128.   {
  129.     decimalOn = false;
  130.     opDisplay.setText(" ");
  131.     memDisplay.setText("(Memory empty)");
  132.     numMemory = 0;
  133.     operatorMemory = -1;
  134.     digitsTyped = 0;
  135.   }
  136.   public void actionPerformed(ActionEvent e) 
  137.   {
  138.  
  139.     if((e.getActionCommand().equals(".")) && (!(decimalOn)))
  140.       {
  141.     decimalOn = true;
  142.     output.setText(output.getText() + ".");    
  143.       }
  144.     else if(e.getActionCommand().equals("Clear"))
  145.       {
  146.     output.setText("0");
  147.     clear();
  148.       }
  149.     else if(e.getActionCommand().equals("Cancel"))
  150.       {
  151.     useValue = false;
  152.     (getParent()).setVisible(false);
  153.       }
  154.     // else if(e.getActionCommand().equals("OK"))
  155. //       {
  156. //     useValue = true;
  157. //     (getParent()).setVisible(false);
  158.     
  159. //     // this is a TEMPORARY fix! the Calculator class is intended to be a four function
  160. //     // calculator for use in any application that needs it, not necceassrily tied to a
  161. //     // MoneyChooser. this is how it is for now because i need WindowListeners on 
  162. //     // JPopupMenus.
  163.     
  164. //     myMoneyChooser.getCalculatorInfo();    
  165. //       }
  166.     else if(e.getActionCommand().equals("="))
  167.       {
  168.     double temp = convertOutput();
  169.     switch(operatorMemory)
  170.       {
  171.       case 0:
  172.         output.setText(Double.toString(temp*numMemory));
  173.         clear();
  174.         break;
  175.       case 1:
  176.         output.setText(Double.toString(numMemory/temp));
  177.         clear();
  178.         break;
  179.       case 2:
  180.         output.setText(Double.toString(temp+numMemory));
  181.         clear();
  182.         break;
  183.       case 3:
  184.         output.setText(Double.toString(numMemory-temp));
  185.         clear();
  186.         break;
  187.       }
  188.     if(output.getText().indexOf('.') != -1)
  189.       {
  190.         decimalOn = true;
  191.       }
  192.       }
  193.   }
  194.   public JButton getOKButton()
  195.   {
  196.     return OK;
  197.   }
  198.  
  199.   class numArrayListener implements ActionListener
  200.   {
  201.     public numArrayListener(){}
  202.     public void actionPerformed(ActionEvent e) 
  203.     {
  204.       if(((!(decimalOn)) && (digitsTyped < 9)) || ((decimalOn) && (digitsTyped <15)))
  205.     {
  206.       digitsTyped++;
  207.       if(output.getText() == "0")
  208.         {
  209.           output.setText(Integer.toString(((indexedButton)(e.getSource())).index));
  210.         }
  211.       else
  212.         {
  213.           output.setText(output.getText() + Integer.toString(((indexedButton)(e.getSource())).index));
  214.         }
  215.       
  216.     }
  217.     }
  218.   } 
  219.   class opArrayListener implements ActionListener
  220.   {
  221.     public opArrayListener(){}
  222.     public void actionPerformed(ActionEvent e)
  223.     {     
  224.       operatorMemory =((indexedButton)(e.getSource())).index; 
  225.       opDisplay.setText(((indexedButton)(e.getSource())).getText());
  226.       memDisplay.setText(output.getText());
  227.       numMemory = convertOutput();
  228.       output.setText("");
  229.       decimalOn = false;
  230.       digitsTyped = 0;
  231.     }  
  232.   }
  233.   class indexedButton extends JButton
  234.   {
  235.     public int index;
  236.     public indexedButton(String txt, int ind)
  237.     {
  238.       super(txt);
  239.       index = ind;
  240.     }
  241.   }
  242. }
  243.  
  244.  
  245.  
  246.  
  247.